package com.laowang.framework.security.service;

import com.laowang.common.enums.UserStatus;
import com.laowang.common.exception.BaseException;
import com.laowang.common.utils.MobileCheck;
import com.laowang.common.utils.StringUtils;
import com.laowang.framework.security.LoginUser;

import com.laowang.project.system.domain.SysUser;
import com.laowang.project.system.domain.WxUser;

import com.laowang.project.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户验证处理
 *
 * @author laowang
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;


    @Autowired
    private SysPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //这里有可能是前端的用户登录(手机号) 也有可能是后台的用户
        SysUser user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            log.info("登录用户：{} 不存在.", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户：{} 已被删除.", username);
            throw new BaseException("对不起,您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 已被停用.", username);
            throw new BaseException("对不起,您的账号：" + username + " 已停用");
        }

        return createLoginUser(user);
        /*boolean mobile = MobileCheck.isMobile(username);
        if (mobile){
            AppUser appUser = appUserService.selectByPhone(username);
            if (StringUtils.isNull(appUser)) {
                log.error("手机号还未在注册:{}.", username);
                throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
            } else if (appUser.getIsDelete().equals(1)) {
                log.error("登录手机号：{} 已被删除.", username);
                throw new BaseException("对不起,您的手机号：" + username + " 已被删除");
            }
            UserDetails loginAppUser = createLoginAppUser(appUser);
            return loginAppUser;
        }else {
            SysUser user = userService.selectUserByUserName(username);
            if (StringUtils.isNull(user)) {
                log.info("登录用户：{} 不存在.", username);
                throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
            } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
                log.info("登录用户：{} 已被删除.", username);
                throw new BaseException("对不起,您的账号：" + username + " 已被删除");
            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("登录用户：{} 已被停用.", username);
                throw new BaseException("对不起,您的账号：" + username + " 已停用");
            }

            return createLoginUser(user);
        }*/


    }

    private UserDetails createLoginAppUser(WxUser wxUser) {
        return new LoginUser(wxUser,"wxUser");
    }

    public UserDetails createLoginUser(SysUser user) {
        return new LoginUser(user, permissionService.getMenuPermission(user),"pc");
    }
}
